package com.secneo.appshare.threads;

import java.io.IOException;
import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;

import com.secneo.appshare.R;
import com.secneo.appshare.api.impl.RemoteApiImpl;
import com.secneo.appshare.common.Constants;
import com.secneo.appshare.common.Util;
import com.secneo.appshare.privatemsg.PrivateMsg;

public class PrivateMsgThread extends Thread{
	
	private String mUid = null ;	//uid
	private String mOUid = null ;
	private Context mContext = null ;
	private Handler mHandler = null ;
	private List<PrivateMsg> mPrivateMsgList = null ;	//分页显示，最新返回的
	private boolean mRun ;
	private boolean mIsByUid ;	//时候根据UID取私信列表
	private int mPageIndex = 0 ;
	
	public PrivateMsgThread(Context context , Handler handler , String uid ,
			String o_uid , boolean isByUid , int pageIndex){
		mContext = context ;
		mHandler = handler ;
		mUid = uid ;
		mRun = false ;
		mOUid = o_uid ;
		mIsByUid = isByUid ;
		mPageIndex = pageIndex ;
	}
	
	public synchronized void stopRun(){
		this.interrupt() ;
		this.stop() ;
		mRun = false ;
	}
	
	public List<PrivateMsg> getList(){
		return mPrivateMsgList ;
	}
	
	private void sendMessage(int what){
		Message msg = new Message() ;
		msg.what = what ;
		mHandler.sendMessage(msg) ;
	}
	
	public void run(){
		mRun = true ;
		
		RemoteApiImpl api_impl = new RemoteApiImpl() ;
		
		//下载文字信息
		//TODO mUid for test
//		mUid = "191723e832f24b998a588919939247c3" ;
		if(!mIsByUid)
			mPrivateMsgList = api_impl.recieveMsg(mContext , mUid , mPageIndex) ;
		else
			mPrivateMsgList = api_impl.recieveMsgByUid(mContext , mUid, mOUid , mPageIndex) ;		
		
		if(mPrivateMsgList == null){
			if(!mIsByUid)
				sendMessage(Constants.GET_PRIVATE_MSG_ERR) ;
			else
				sendMessage(Constants.GET_PRIVATE_MSG_BY_UID_ERR) ;
			return ;
		}
		
		int count = mPrivateMsgList.size() ;
		
		if(count == 0){
			if(!mIsByUid)
				sendMessage(Constants.GET_PRIVATE_MSG_NO_DATA) ;
			else
				sendMessage(Constants.GET_PRIVATE_MSG_BY_UID_NO_DATA) ;
			return ;
		}
		
		//文字信息下载完毕
		if(!mIsByUid)
			sendMessage(Constants.GET_PRIVATE_MSG_FINISH) ;
		else
			sendMessage(Constants.GET_PRIVATE_MSG_BY_UID_FINISH) ;
		
		//下载图片
		for(int i = 0 ; i < count ; ++i)
		{
			PrivateMsg privateMsg = mPrivateMsgList.get(i) ;
			if(privateMsg.getO_icon() == null)
			{
				try 
				{
					String iconUrl = privateMsg.getO_iconurl();
					if(iconUrl.contains("buildin"))
						privateMsg.setO_icon(Util.getDrawableFromAsset(mContext, iconUrl)) ;
					else
					{
						Drawable d_tmp = Util.getDrawableFromCache(mContext, iconUrl);
						if(d_tmp == null)
							privateMsg.setO_icon(mContext.getResources().getDrawable(R.drawable.some_user)) ;
						else
							privateMsg.setO_icon(d_tmp) ;
					}
				
				} catch (IOException e) {
					privateMsg.setO_icon(mContext.getResources().getDrawable(R.drawable.some_user)) ;
					e.printStackTrace();
				}
			}
		//图片下载完毕
		if(!mIsByUid)
			sendMessage(Constants.GET_PRIVATE_MSG_IMG_FINISH) ;
		else
			sendMessage(Constants.GET_PRIVATE_MSG_IMG_BY_UID_FINISH) ;
		}
	}	
}
